//
//  OADepAddressController.m
//  OASystem
//
//  Created by zhoupengfei on 16/1/17.
//  Copyright © 2016年 zpf. All rights reserved.
//

#import "OADepAddressController.h"
#import "HttpTools.h"
#import "MBProgressHUD.h"
#import "OAAddressDetailViewController.h"
#import "Toast+UIView.h"
@interface DepAddressCellList : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UILabel *subject;
@property (weak, nonatomic) IBOutlet UILabel *des;


@end

@implementation DepAddressCellList



@end

@interface OADepAddressController ()

@end

@implementation OADepAddressController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    return _contents;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.contents.count == 0) {
           [self requestData];
    }
    //[self requestData];
    
    [self setRightItem];
}

-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}


-(void)searchAction{
    
    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}
//搜素通讯录
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"condition":textField.text},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:@"adressListSearch" success:^(id JSON) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            
            NSMutableArray * tempArray = JSON[@"data"];
            
            if (tempArray.count) {
                self.contents = tempArray;
            }
            if (self.contents.count == 0) {
                [self.view makeToast:@"搜索无相关数据"];
            }
            
            [self.tableView reloadData];
            
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    }
}


-(void)requestData{
      [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSArray *  array = @[@{@"path":self.path}];
    [HttpTools requestWithURL:KExchother arrays:array method:KDepTxlTree success:^(id JSON) {
        
        NSLog(@"%@",JSON);
        
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
//        self.contents = dict[@"PartNode"];
        for (NSDictionary *dict1 in dict[@"PartNode"]) {
            if (dict1[@"ryxm"] != nil) {
                
                [self.contents addObject:dict1];
                
            }
            
            
        }

        [self.tableView reloadData];
    } failure:^(NSError *error) {
         [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    DepAddressCellList * cell = [tableView dequeueReusableCellWithIdentifier:@"DepCellId" forIndexPath:indexPath];
    NSDictionary *dict = self.contents[indexPath.row];
    cell.title.text = dict[@"surname"];
    cell.subject.text = dict[@"ryxm"];
    cell.des.text = dict[@"duties"];
    cell.title.layer.cornerRadius = 10;
    cell.title.layer.masksToBounds = YES;
    cell.title.backgroundColor = [self randomColor];
    return cell;
}

-(UIColor *) randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"AnJiMain" bundle:nil];
    OAAddressDetailViewController * detailVC = (OAAddressDetailViewController*)[storyBoard instantiateViewControllerWithIdentifier:@"AddressDetailId"];
     NSDictionary *dict = self.contents[indexPath.row];
    detailVC.unid = dict[@"unid"];
    [self.navigationController pushViewController:detailVC animated:YES];
}

@end
